# -*- coding:utf-8 -*-

import unittest
import time

class FileCount:
    def __init__(self):
        """
            文件引用计数
        """
        self.file_count_map = {}
        
        """
            文件引用计算器
            add by wshi 2012-09-22
        """
        self.file_time_map = {}
        
    def add_file(self, filename, count):
        file_count = self.file_count_map.get(filename)
        if file_count:
            self.file_count_map[filename] = file_count + count
        else:
            self.file_count_map[filename] = count
            self.file_time_map[filename] = int(time.time())
    
    '''
    设置文件引用计数
    filename：文件名
    count：引用计数
    add by wshi 2012-09-22
    '''
    def set_file(self, filename, count):
        self.file_count_map[filename] = count
        if not filename in self.file_time_map:
            self.file_time_map[filename] = int(time.time())
            
    def file_dec(self, filename):
        file_count = self.file_count_map.get(filename)
        if file_count:
            self.file_count_map[filename] = file_count - 1
    
    def get_free_file(self):
        tmp_file_count = self.file_count_map
        self.file_count_map = {}
        free_file_count = {}
        
        tmp_file_time = self.file_time_map
        self.file_time_map = {}
        
        for key in tmp_file_count:
            #if tmp_file_count[key]:
            if tmp_file_count[key] > 0:
                self.file_count_map[key] = tmp_file_count[key]
                self.file_time_map[key] = tmp_file_time[key]
            else:
                free_file_count[key] = tmp_file_count[key]
        return free_file_count
    
    '''
        判断文件是否超时
        overtime:超时时长
        filename：文件名
        返回：ture：超时，false：不是超时
        add by wshi 2012-09-22
    '''
    def is_overtime_file(self,filename,overtime):
        if filename in self.file_time_map:
            if (time.time() - self.file_time_map[filename]) < overtime:
                return False
            
        return True
        
    def print_all(self):
        for key in self.file_count_map.keys():
            print "(", key, ", ",  self.file_count_map[key], ")"
    

class FileCountTestCase(unittest.TestCase):
    def setUp(self):
        self.filecount = FileCount()
        
    def tearDown(self):
        pass
        
    def testZeroReference(self):
        self.filecount.addFile("MASS_1261445912_001927_Mail_440300_111.bcp", 2)
        self.filecount.PrintAll()
        self.filecount.FileDEC("MASS_1261445912_001927_Mail_440300_111.bcp")
        self.filecount.FileDEC("MASS_1261445912_001927_Mail_440300_111.bcp")
        self.filecount.FileDEC("MASS_1261445912_001927_Mail_440300_111.bcp")
        self.assertTrue(self.filecount.getFreeFiles())
        
    def testUpDateSpeed(self):
        start_time = time.time()
        for seq in range(100000):
            self.filecount.addFile("MASS_1261445909_%s_IM_440300_111.bcp" % (seq), 2)
    
        for seq in range(10000):
            seq += 2000
            self.filecount.FileDEC("MASS_1261445909_%s_IM_440300_111.bcp" % (seq))
        
        for seq in range(40000):
            seq += 1000
            self.filecount.FileDEC("MASS_1261445909_%s_IM_440300_111.bcp" % (seq))        
        
        result = self.filecount.getFreeFiles() 
        
        end_time = time.time()
        
        print "len:", len(result), "time:", end_time - start_time           
        
if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest(FileCountTestCase("testUpDateSpeed"))
    
    runner = unittest.TextTestRunner()
    runner.run(suite)
 